package com.kitty.game;

import com.kitty.common.cache.RefreshCache;
import com.kitty.common.schedule.task.ChongZhiRankTask;
import com.kitty.common.schedule.task.ChouJiangRankTask;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.achieve.common.AchieveConfigInfo;
import com.kitty.game.attribute.AttrService;
import com.kitty.game.base.service.CommonService;
import com.kitty.game.boss.service.NewBossService;
import com.kitty.game.config.Reloadable;
import com.kitty.game.drop.service.DropService;
import com.kitty.game.house.service.HouseService;
import com.kitty.game.hunpo.service.HunPoService;
import com.kitty.game.item.service.CangBaoXiangHandler;
import com.kitty.game.item.service.ChangeCardService;
import com.kitty.game.item.service.PetStoneService;
import com.kitty.game.neidan.service.NeidanService;
import com.kitty.game.newchoujiang.service.NewChouJiangService;
import com.kitty.game.rank.facade.RankController;
import com.kitty.game.role.service.AccountService;
import com.kitty.game.school.service.SchoolService;
import com.kitty.game.shop.ShopService;
import com.kitty.game.talk.service.TalkService;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.titleManage.service.TitleInfoService;
import com.kitty.game.welfare.service.RechargeScoreRewardHandler;
import com.kitty.game.welfare.service.ServenDayRewardHandler;
import com.kitty.game.wuhun.WuHunService_;
import com.kitty.game.reward.service.RewardSetService;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 产品数据
 */
public enum ProductDataPool {
    INSTANCE; //单例


    private ConcurrentMap<Class<?>, Reloadable> datas = new ConcurrentHashMap<>();

    /**
     * 加载产品数据
     */
    public void load() {
        //加载封号信息

        SpringUtils.getBean(AccountService.class).getStopAccount();
        /**加载commonset表数据*/
        SpringUtils.getBean(CommonService.class).loadProduct();

        /**加载任务产品数据*/
        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        taskService.loadTaskSet();

        /**加载对话产品数据*/
        TalkService talkService = SpringUtils.getBean(TalkService.class);
        talkService.loadTalkSet();

        /**加载师门数据*/
        SchoolService schoolService = SpringUtils.getBean(SchoolService.class);
        schoolService.loadSchoolSet();

        NewBossService bossService = SpringUtils.getBean(NewBossService.class);
        bossService.loadProductData();

        /**加载属性配置*/
        SpringUtils.getBean(AttrService.class).initAttribute();

        /**加载宠物妖石数据*/
        SpringUtils.getBean(PetStoneService.class).init();

        /**加载杂货店数据*/
        SpringUtils.getBean(ShopService.class).init();

        /**加载变身卡数据*/
        SpringUtils.getBean(ChangeCardService.class).init();

        /**加载指引数据*/
        SpringUtils.getBean(NewTaskService.class).initInstruction();

        /**加载掉落数据*/
        SpringUtils.getBean(DropService.class).loadProduct();
        /**加载redis缓存数据*/
        SpringUtils.getBean(RefreshCache.class).refreshRedisInfo();
        /**加载称号数据*/
        SpringUtils.getBean(TitleInfoService.class).loadProduct();

        /**加载充值奖励数据*/
        SpringUtils.getBean(RewardSetService.class).loadProduct();

        /**加载神魂数据*/
        SpringUtils.getBean(HunPoService.class).loadShenHun();
        /**加载武魂数据*/
        SpringUtils.getBean(WuHunService_.class).loadWuhun();
        /**加载内丹数据*/
        SpringUtils.getBean(NeidanService.class).loadWuhun();

        /**加载积分抽奖数据*/
        SpringUtils.getBean(NewChouJiangService.class).load();
        SpringUtils.getBean(RechargeScoreRewardHandler.class).loadProduct();

        //加载七日登录
        SpringUtils.getBean(ServenDayRewardHandler.class).load();

        //加载藏宝箱
        SpringUtils.getBean(CangBaoXiangHandler.class).loadCommonSet();

        //SpringUtils.getBean(BroadcastService.class).findAllZhanLi();

        //加载首次称号
        SpringUtils.getBean(RankController.class).loadCommonSet();

        //加载成就
        SpringUtils.getBean(AchieveConfigInfo.class).loadCommonSet();

        //重启充值排行榜
        SpringUtils.getBean(ChongZhiRankTask.class).rebootExecute();

        //重启抽奖排行榜
        SpringUtils.getBean(ChouJiangRankTask.class).rebootExecute();

        //加载家具
        SpringUtils.getBean(HouseService.class).initFurniture();
        Map<String, Reloadable> storages = SpringUtils.getBeansOfType(Reloadable.class);
        for (Map.Entry<String, Reloadable> entry : storages.entrySet()) {
            Reloadable reloadable = entry.getValue();
            reloadable.reload();
            datas.put(reloadable.getClass(), reloadable);
        }

    }

    /**
     * 单表重载
     * @param configTableName 配置表名称
     */
    public String reload(String configTableName) {
        if (configTableName.contains("p_")) {
            configTableName = configTableName.replace("p_", "");
        }
        for (Map.Entry<Class<?>, Reloadable> entry : datas.entrySet()) {
            Class<?> c = entry.getKey();
            if (c.getSimpleName().toLowerCase().indexOf(configTableName.toLowerCase()) >= 0) {
                try {
                    Reloadable storage = entry.getValue();
                    storage.reload();
                    return "重载表成功-->" + configTableName;
                } catch (Exception e) {

                    return "重载表失败-->" + configTableName;
                }
            }
        }
        return configTableName + "表不存在";
    }
}
